package machinery;

import machinery.state.MachineryState;
import machinery.state.IdleState;
import java.util.Map;

/**
 * 观察者模式：农机设备作为观察者
 */
public abstract class Machinery {
    protected String name;
    protected String type;
    protected double fuel;
    protected double[] position;
    protected MachineryState state;
    
    public Machinery(String name, String type) {
        this.name = name;
        this.type = type;
        this.fuel = 100.0; // 初始油量
        this.position = new double[]{0.0, 0.0}; // 初始位置
        this.state = new IdleState(); // 状态模式：初始状态为空闲
    }
    
    // 观察者模式：接收环境变化通知并响应
    public void update(Map<String, Object> environmentData) {
        System.out.println(name + "接收到环境变化: " + environmentData);
        
        // 根据环境数据调整工作状态
        adjustWorkingParameters(environmentData);
    }
    
    // 状态模式：改变农机状态
    public void changeState(MachineryState newState) {
        System.out.println(name + "状态从 " + state.getStateName() + " 变更为 " + newState.getStateName());
        this.state = newState;
        this.state.handle(this);
    }
    
    // 根据环境数据调整工作参数
    protected abstract void adjustWorkingParameters(Map<String, Object> environmentData);
    
    // 执行作业任务
    public void executeTask(String taskName) {
        System.out.println(name + " 执行任务: " + taskName);
        state.handle(this); // 通过当前状态处理任务
    }
    
    // Getter和Setter方法
    public String getName() {
        return name;
    }
    
    public String getType() {
        return type;
    }
    
    public double getFuel() {
        return fuel;
    }
    
    public double[] getPosition() {
        return position;
    }
    
    public void setPosition(double[] position) {
        this.position = position;
    }
    
    public MachineryState getState() {
        return state;
    }
} 